<!-- 修改时间 -->
<template>

<div>
  <div class="loader-base" v-if="showLoad"><div class="loader-wrap"></div></div>
  <!-- <p class="tip">—— 请选择提货时间 ——</p>
  <div class="data-box data1">
    <datetime v-model="value1" format="YYYY-MM-DD HH:mm" ref="datetime1"
      year-row="{value}年" month-row="{value}月" day-row="{value}日" hour-row="{value}点" minute-row="{value}分"></datetime>
  </div>
  <p class="tip">—— 请选择到达时间 ——</p>
  <div class="data-box data2">
    <datetime v-model="value2" format="YYYY-MM-DD HH:mm" ref="datetime2"
      year-row="{value}年" month-row="{value}月" day-row="{value}日" hour-row="{value}点" minute-row="{value}分"></datetime>
  </div> -->
  <div class="form-box displayflex">
    <div class="label">提货时间</div>
    <div class="ctx flex1 txt-rit">
      <datetime v-model="value1" 
         format="YYYY-MM-DD HH:mm"
         year-row="{value}年" month-row="{value}月" day-row="{value}日" hour-row="{value}点" minute-row="{value}分">{{value1}}</datetime>
    </div>
    <i class="iconfont icon-right"></i>
  </div>
  <div class="form-box displayflex">
    <div class="label">到达时间</div>
    <div class="ctx flex1 txt-rit">
      <datetime v-model="value2" 
         format="YYYY-MM-DD HH:mm"
         year-row="{value}年" month-row="{value}月" day-row="{value}日" hour-row="{value}点" minute-row="{value}分">{{value2}}</datetime>
    </div>
    <i class="iconfont icon-right"></i>
  </div>
  <div class="form-box displayflex">
    <div class="label">重量</div>
    <div class="ctx flex1">
      <input type="number" class="txt-rit" placeholder="请输入" v-model="OrdersWeight">
    </div>
    <i class="rit">Kg</i>
  </div>
  <div class="form-box displayflex">
    <div class="label">体积</div>
    <div class="ctx flex1">
      <input type="number" class="txt-rit" placeholder="请输入" v-model="OrdersVolumn">
    </div>
    <i class="rit">m³</i>
  </div>
  <div class="form-box displayflex">
    <div class="label">运费</div>
    <div class="ctx flex1 org">
      <input type="number" readonly class="txt-rit" placeholder="请输入" v-model="CarrierAmount">
    </div>
    <i class="rit org">元</i>
  </div>
  <div class="displayflex form-box" v-if="classfiyList" >
    <popup-picker class="picker-wrap" 
        title="绑定设备" 
        :data="classfiyList" 
        placeholder="点击绑定" 
        value-text-align="right"
        :show-name="classfiyshow"
        v-model="classfiy"></popup-picker>
    <i class="iconfont icon-right"></i>
  </div>
  
  <div class="form-box displayflex" v-else>
    <div class="label">绑定设备</div>
    <div class="ctx flex1 txt-rit">暂无设备
    </div>
  </div>

  <div class="txt-rit" style="display:none" id="DepartureVenue">{{FHAddress.Province | province}}/{{FHAddress.City | city}}/{{FHAddress.District | district}}</div>
  <div class="txt-rit" style="display:none" id="ArrivalVenue">{{SHAddress.Province | province}}/{{SHAddress.City | city}}/{{SHAddress.District | district}}</div>


  <div class="btn-double displayflex">
    <div class="flex1" @click="cancel()">取消</div>
    <div class="flex1" @click="sure()">确定</div></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      value1: "",
      value2: "",
      showLoad: false,
      OrdersWeight: 0,
      OrdersVolumn: 0,
      FHAddress: [],
      SHAddress: [],
      CarrierAmount: 0,
      AdditionalCost: 0,
      OrderAmount: 0,
      //绑定设备
      classfiyshow: true,
      classfiy: [],
      classfiyList: [
        // [
        //   {
        //     value: "warehouse",
        //     name: "仓管员"
        //   },
        //   {
        //     value: "Cargoreceiver",
        //     name: "收货员"
        //   }
        // ]
      ],
      Mfcoldequipment:[],
    };
  },
  computed: {
    userinfo() {
      if (localStorage.UserInfo) {
        return JSON.parse(localStorage.UserInfo);
      }
    }
  },
  watch: {
    OrdersWeight(val) {
      // console.log(1);
      var self = this;
      // console.log(self.FHAddress);
      var fhadd = self.FHAddress.Venue;
      var shadd = self.SHAddress.Venue;
      if(fhadd == null)
      {
        fhadd = document.getElementById('DepartureVenue').innerHTML;
      }
      if(shadd == null)
      {
        shadd = document.getElementById('ArrivalVenue').innerHTML;
      }
      var origin = fhadd.replace(/\//g, "") + self.FHAddress.Address;
      var destinat = shadd.replace(/\//g, "") + self.SHAddress.Address;
      var query = {
        weight: val,
        standardWeigth: 20,
        origin: origin,
        destinat: destinat
      };
      this.$vux.loading.show({ transition:'',text: '运费计算中...' });
      this.Ajax.post("GetFreight", query).then(function(resp) {
        if (resp.IsSuccess) {
          // console.log(resp.Data.CarrierAmount);
          self.CarrierAmount = resp.Data.CarrierAmount;
          self.AdditionalCost = resp.Data.AdditionalCost;
          self.OrderAmount = resp.Data.OrderAmount;
          self.$vux.loading.hide();
        }
      });
    }
  },
  created() {
    this.getData();
    this.getMfDeviceOrderAssociation();
  },
  methods: {
    getData() {
      var self = this;
      var guid = self.$route.query.BBillGuid;
      var query = {
        Guid: guid
      };
      self.showLoad = true;
      self.Ajax.post("GetOrderDetail", query).then(function(resp) {
        if (resp.IsSuccess) {
          var trpp = resp.Data.Main;
          self.OrdersWeight = trpp.OrdersWeight;
          self.OrdersVolumn = trpp.OrdersVolumn;
          self.CarrierAmount = trpp.CarrierAmount;
          var SHAddress = {
            Province: trpp.DepartureProvince,
            City: trpp.DepartureCity,
            District: trpp.DepartureDistrict,
            Address: trpp.DepartureAddress,
            Contacts: trpp.PickContacts,
            ContactsTel: trpp.PickContactsTel,
            Venue: trpp.DepartureVenue
          };
          var FHAddress = {
            Province: trpp.ArrivalProvince,
            City: trpp.ArrivalCity,
            District: trpp.ArrivalDistrict,
            Address: trpp.ArrivalAddress,
            Contacts: trpp.SendContacts,
            ContactsTel: trpp.SendContactsTel,
            Venue: trpp.ArrivalVenue
          };
          self.FHAddress = SHAddress;
          self.SHAddress = FHAddress;
          self.value1 = trpp.CollectionTime.replace("T", " ").substr(
            0,
            trpp.CollectionTime.length - 3
          );
          self.value2 = trpp.LastArrivalArrivedDate.replace("T", " ").substr(
            0,
            trpp.LastArrivalArrivedDate.length - 3
          );
          // self.$refs.datetime1.render();
          // self.$refs.datetime2.render();
          self.showLoad = false;
        }
      });
    },
    getMfcoldequipment() {
      var self = this;
      self.showLoad = true;
      var query = {
        Type: "4",
        TenantGuid: self.userinfo.TenantGuid
      };
      this.Ajax.post("GetMfcoldequipment", query).then(function(resp) {
        self.showLoad = false;
        if (resp.IsSuccess) {
          var  classfiyListA=[];
          if(resp.Datas.length > 0)
          {
            resp.Datas.forEach(element => {
              var item = {
                value: element.Guid,
                name: element.Name
              };
              classfiyListA.push(item);
            });
          }
          if (self.Mfcoldequipment) {
             var item = {
                value: self.Mfcoldequipment.Guid,
                name: self.Mfcoldequipment.Name
              };
              classfiyListA.push(item);
          }
          self.classfiyList.push(classfiyListA);
        }
      });
    },
    getMfDeviceOrderAssociation()
    {
      var self = this;
      var guid = self.$route.query.guid;
      var query ={
        OrderGuid:guid
      };
       this.Ajax.post("GetMfcoldequipment", query).then(function(resp) {
        if (resp.IsSuccess) {
         self.Mfcoldequipment =resp.Data;
         if (resp.Data) {
            self.classfiy.push(resp.Data.Guid);
         }
        
        }
         self.getMfcoldequipment();
      });
    },
    sure() {
      var self = this;
        self.showLoad = true;
      var guid = self.$route.query.guid;
      var BBillGuid = self.$route.query.BBillGuid;
      if (self.value2 < self.value1) {
        self.$vux.toast.show({
          type: "warn", //success, warn, cancel, text
          text: "要求到达时间要大于提货时间。"
        });
        return false;
      }
      //平台收益/配送员收益
      var PlatformAmount = 0;
      var DriverAmount = 0;
      if (self.OrderAmount == 30) {
        PlatformAmount = 15 + self.AdditionalCost * 0.5;
        DriverAmount = 15 + self.AdditionalCost * 0.5;
      }

      if (self.OrderAmount == 50) {
        PlatformAmount = 30 + self.AdditionalCost * 0.5;
        DriverAmount = 20 + self.AdditionalCost * 0.5;
      }
      if (self.OrderAmount == 70) {
        PlatformAmount = 20 + self.AdditionalCost * 0.5;
        DriverAmount = 50 + self.AdditionalCost * 0.5;
      }
      var OldcoldequipmentGuid='';
      if (self.Mfcoldequipment) {
        OldcoldequipmentGuid = self.Mfcoldequipment.Guid;
      }
      // var curTime = this.getCurrentDate();
      var query = {
        Guid: guid,
        BBillGuid: BBillGuid,
        CollectionTime: self.value1,
        LastArrivalArrivedDate: self.value2,
        PlatformIncome: PlatformAmount,
        DistributionIncome: DriverAmount,
        OrdersVolumn: self.OrdersVolumn,
        CarrierAmount: self.CarrierAmount,
        OrdersWeight: self.OrdersWeight,
        coldequipmentGuid:self.classfiy[0],
        OldcoldequipmentGuid:OldcoldequipmentGuid
      };
      this.Ajax.post("EditOrderTime", query).then(function(resp) {
        if (resp.IsSuccess) {
         
          self.$vux.toast.show({
            type: "text",
            width: "auto",
            text: "修改成功"
          });
          self.$router.push({
            path: "/orderList"
          });
        } else {
          self.$vux.toast.show({
            type: "text",
            width: "auto",
            text: resp.ErrorMessage
          });
        }
           self.showLoad = false;
      });
    },
    cancel() {
      this.$router.push({
        path: "/orderList"
      });
    }
  }
};
</script>